package cn.easyshipping.cloud.common.util;

import cn.easyshipping.cloud.business.dto.ResponsePageResult;
import cn.easyshipping.cloud.business.dto.ResponseResult;
import cn.easyshipping.cloud.common.constant.EnumApiResult;
import cn.easyshipping.cloud.common.constant.ErrorCode;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ResponseUtil {
    public <T> ResponseResult<T> successResult(T resultObject) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setResult(EnumApiResult.Success.getValue());
        result.setResultMessage("1000");//成功
        result.setTS((new Date()).getTime());
        result.setReturnValue(resultObject);
        return result;
    }
    public <T> ResponsePageResult<T> successPageResult(T resultObject, int count) {
        ResponsePageResult<T> result = new ResponsePageResult<>();
        result.setResult(EnumApiResult.Success.getValue());
        result.setResultMessage("1000");//成功
        result.setTS((new Date()).getTime());
        result.setReturnValue(resultObject);
        result.setCount(count);
        return result;
    }

    public ResponseResult<String> failedResult(String errorCode, String errorMsg) {
        ResponseResult<String> result = new ResponseResult<>();
        result.setResult(EnumApiResult.Fail.getValue());
        result.setResultMessage(errorCode);
        result.setTS((new Date()).getTime());
        result.setReturnValue(errorMsg);
        return result;
    }

    public ResponseResult<String> failedResult(ErrorCode errorCode) {
        ResponseResult<String> result = new ResponseResult<>();
        result.setResult(EnumApiResult.Fail.getValue());
        result.setResultMessage(errorCode.getErrorCode());
        result.setTS((new Date()).getTime());
        result.setReturnValue(errorCode.getErrorMsg());
        return result;
    }

    public <T> ResponseResult<T> failedResult(ErrorCode errorCode, T resultObject) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setResult(EnumApiResult.Fail.getValue());
        result.setResultMessage(errorCode.getErrorCode());
        result.setTS((new Date()).getTime());
        result.setReturnValue(resultObject);
        return result;
    }

    public <T> ResponseResult<T> responseResult(int result, String resultMessage, T resultObject) {
        ResponseResult<T> resResult = new ResponseResult<>();
        resResult.setResult(result);
        resResult.setResultMessage(resultMessage);
        resResult.setTS((new Date()).getTime());
        resResult.setReturnValue(resultObject);
        return resResult;
    }
}
